package com.leap.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leap.entity.Admin;
import com.leap.entity.Article;
import com.leap.entity.Media;
import com.leap.entity.RestBean;
import com.leap.entity.vo.ArticleCategoryList;
import com.leap.entity.vo.ArticleVO;
import com.leap.entity.vo.ArticleVOList;
import com.leap.service.AdminService;
import com.leap.service.ArticleService;
import com.leap.service.MediaService;
import com.leap.utils.Const;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/admin")
@Tag(name = "管理员相关接口")
public class AdminController {

    private static final Logger log = LoggerFactory.getLogger(AdminController.class);
    @Autowired
    private ArticleService articleService;

    @Autowired
    private MediaService mediaService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 管理员获取后台文件集合
     * @param page
     * @param isVideo
     * @return
     */
    @Operation(summary = "管理员获取后台文件集合接口")
    @GetMapping("/list")
    public RestBean<ArticleCategoryList> getArticleList(int page, int isVideo, int isPublish,
                                                        @RequestParam(defaultValue = "0") int category) {

        Page<Article> pageInfo = new Page<>(page, Const.ADMIN_PAGE_SIZE);

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getIsVideo, isVideo);
        queryWrapper.eq(Article::getIsPublish,isPublish);
        queryWrapper.orderByAsc(Article::getUpdateTime);
        if (category != 0){
            queryWrapper.eq(Article::getCategoryId, category);
        }

        articleService.page(pageInfo, queryWrapper);

        List<Article> articleList = pageInfo.getRecords();

        List<ArticleVO> articleListVO = articleList.stream().map(item -> {
            Long articleId = item.getId();
            Media media = mediaService.getCoverById(articleId);
                return ArticleVO.builder()
                        .articleId(articleId)
                        .title(item.getTitle())
                        .category(item.getCategoryId())
                        .author(item.getAuthorName())
                        .coverUrl(media==null ? null : media.getMediaUrl())
                        .browseNum(item.getBrowseNum())
                        .likeNum(item.getLikeNum())
                        .duration(media==null ? 0L : media.getDuration())
                        .isVideo(item.getIsVideo())
                        .isLiked(0)
                        .updateTime(item.getUpdateTime())
                        .build();
        }).toList();
        int articleSum = 0;
        int videoSum = 0;
        int c1_articleSum = 0;
        int c2_articleSum = 0;
        int c3_articleSum = 0;
        int c4_articleSum = 0;

        int c1_videoSum = 0;
        int c2_videoSum = 0;
        int c3_videoSum = 0;
        int c4_videoSum = 0;
        if(isPublish == 0){
            //未发布统计
            String nota = Const.NOT_PUBLISH_ARTICLE_SUM;
            String notv = Const.NOT_PUBLISH_VIDEO_ARTICLE_SUM;

            String notca1 = Const.NOT_PUBLISH_CATEGORY_1_ARTICLE_SUM;
            String notca2 = Const.NOT_PUBLISH_CATEGORY_2_ARTICLE_SUM;
            String notca3 = Const.NOT_PUBLISH_CATEGORY_3_ARTICLE_SUM;
            String notca4 = Const.NOT_PUBLISH_CATEGORY_4_ARTICLE_SUM;

            String notcv1 = Const.NOT_PUBLISH_CATEGORY_1_VIDEO_SUM;
            String notcv2 = Const.NOT_PUBLISH_CATEGORY_2_VIDEO_SUM;
            String notcv3 = Const.NOT_PUBLISH_CATEGORY_3_VIDEO_SUM;
            String notcv4 = Const.NOT_PUBLISH_CATEGORY_4_VIDEO_SUM;

            Object na = redisTemplate.opsForValue().get(nota);
            Object nv = redisTemplate.opsForValue().get(notv);
            Object nca1 = redisTemplate.opsForValue().get(notca1);
            Object nca2 = redisTemplate.opsForValue().get(notca2);
            Object nca3 = redisTemplate.opsForValue().get(notca3);
            Object nca4 = redisTemplate.opsForValue().get(notca4);

            Object ncv1 = redisTemplate.opsForValue().get(notcv1);
            Object ncv2 = redisTemplate.opsForValue().get(notcv2);
            Object ncv3 = redisTemplate.opsForValue().get(notcv3);
            Object ncv4 = redisTemplate.opsForValue().get(notcv4);

            if (na != null || nv != null || nca1 != null || nca2 != null || nca3 != null || nca4 != null
            || ncv1 != null || ncv2 != null || ncv3 != null || ncv4 != null) {
                articleSum = na == null ? 0 : Integer.parseInt(na.toString());
                videoSum = nv == null ? 0 : Integer.parseInt(nv.toString());
                c1_articleSum = nca1 == null ? 0 : Integer.parseInt(nca1.toString());
                c2_articleSum = nca2 == null ? 0 : Integer.parseInt(nca2.toString());
                c3_articleSum = nca3 == null ? 0 : Integer.parseInt(nca3.toString());
                c4_articleSum = nca4 == null ? 0 : Integer.parseInt(nca4.toString());

                c1_videoSum = ncv1 == null ? 0 : Integer.parseInt(ncv1.toString());
                c2_videoSum = ncv2 == null ? 0 : Integer.parseInt(ncv2.toString());
                c3_videoSum = ncv3 == null ? 0 : Integer.parseInt(ncv3.toString());
                c4_videoSum = ncv4 == null ? 0 : Integer.parseInt(ncv4.toString());
            }else {
                articleService.refreshArticleSumCache();
                throw new StringIndexOutOfBoundsException("未知错误 请稍后重试");
            }
        }
        else {
            //已发布统计
            String article = Const.ARTICLE_SUM;
            String video = Const.VIDEO_ARTICLE_SUM;

            String ca1 = Const.CATEGORY_1;
            String ca2 = Const.CATEGORY_2;
            String ca3 = Const.CATEGORY_3;
            String ca4 = Const.CATEGORY_4;
            String cv1 = Const.CATEGORY_1_VIDEO;
            String cv2 = Const.CATEGORY_2_VIDEO;
            String cv3 = Const.CATEGORY_3_VIDEO;
            String cv4 = Const.CATEGORY_4_VIDEO;

            Object a = redisTemplate.opsForValue().get(article);
            Object v = redisTemplate.opsForValue().get(video);

            Object cas1 = redisTemplate.opsForValue().get(ca1);
            Object cas2 = redisTemplate.opsForValue().get(ca2);
            Object cas3 = redisTemplate.opsForValue().get(ca3);
            Object cas4 = redisTemplate.opsForValue().get(ca4);

            Object cvs1 = redisTemplate.opsForValue().get(cv1);
            Object cvs2 = redisTemplate.opsForValue().get(cv2);
            Object cvs3 = redisTemplate.opsForValue().get(cv3);
            Object cvs4 = redisTemplate.opsForValue().get(cv4);

            if (a != null || v != null || cas1 != null || cas2 != null || cas3 != null || cas4 != null
            || cvs1 != null || cvs2 != null || cvs3 != null || cvs4 != null){
                articleSum = a == null ? 0 : Integer.parseInt(a.toString());
                videoSum = v == null ? 0 : Integer.parseInt(v.toString());

                c1_articleSum = cas1 == null ? 0 : Integer.parseInt(cas1.toString());
                c2_articleSum = cas2 == null ? 0 : Integer.parseInt(cas2.toString());
                c3_articleSum = cas3 == null ? 0 : Integer.parseInt(cas3.toString());
                c4_articleSum = cas4 == null ? 0 : Integer.parseInt(cas4.toString());

                c1_videoSum = cvs1 == null ? 0 : Integer.parseInt(cvs1.toString());
                c2_videoSum = cvs2 == null ? 0 : Integer.parseInt(cvs2.toString());
                c3_videoSum = cvs3 == null ? 0 : Integer.parseInt(cvs3.toString());
                c4_videoSum = cvs4 == null ? 0 : Integer.parseInt(cvs4.toString());
            }else {
                articleService.refreshArticleSumCache();
                throw new StringIndexOutOfBoundsException("未知错误 请稍后重试");
            }

        }

        ArticleCategoryList articleCategoryList = ArticleCategoryList.builder()
                .articleVOList(articleListVO)
                .articleSum(articleSum)
                .videoSum(videoSum)
                .category1(c1_articleSum)
                .category2(c2_articleSum)
                .category3(c3_articleSum)
                .category4(c4_articleSum)
                .category1_video(c1_videoSum)
                .category2_video(c2_videoSum)
                .category3_video(c3_videoSum)
                .category4_video(c4_videoSum)
                .build();

        return RestBean.success(articleCategoryList);
    }

}
